package anatex.semantics;

import java.util.ArrayList;
import java.util.Iterator;

import anatex.domain.CustomLocale;
import anatex.domain.Keyword;
import anatex.translate.TranslatedKeyword;

import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.web.client.RestTemplate;

public class Wikipedia {
	
	public static final int WEIGHT_FOR_CATEGORY = 4;
	public static final int WEIGHT_FOR_SEARCH = 2;
	public static final int WEIGHT_FOR_CATEGORY_TRANSLATION = 3;
	public static final int WEIGHT_FOR_SEARCH_TRANSLATION = 1;
	
	public ArrayList<Object[]> getRelatedCategories (Keyword keyword) {
		ArrayList<Object[]> rc = new ArrayList<Object[]>();
		String wikiLang = keyword.getLocale().getShort_language();
		CustomLocale engLoc = CustomLocale.findCustomLocalesByLanguage_full("english").getSingleResult();
		
		RestTemplate restTemplate = new RestTemplate();
		
		String result = restTemplate.getForObject("http://" + wikiLang + ".wikipedia.org/w/api.php?action=query&prop=categories&titles={titles}&redirects=1&cllimit=20&format=json",
                String.class, keyword.getText().trim());

		JSONObject jo = null;
		
		try {
			jo = new JSONObject(result);
			
			JSONObject j = jo.getJSONObject("query").getJSONObject("pages");
			
			if (null != j) {
				
				Iterator<String> i = (Iterator<String>)j.keys();
				
				while (i.hasNext()) {
					JSONObject oj = j.getJSONObject(i.next());
					
					try {
						System.out.println(oj.toString());
						JSONArray ja = oj.getJSONArray("categories");
						
						for (int ii = 0; ii < ja.length(); ii ++) {
							String s = ja.getJSONObject(ii).getString("title");
							String[] ss = s.split(":");
							
							rc.add(new Object[]{s, WEIGHT_FOR_CATEGORY, keyword.getLocale()});
						}
					} catch (Exception ex) {
						System.out.println(ex.getMessage());
					}
					
					break;
				}
				
			} else {
				//no related categories - perform search and get titles
				
				result = restTemplate.getForObject("http://" + wikiLang + ".wikipedia.org/w/api.php?action=query&list=search&format=json&srsearch={keyword}",
		                String.class, keyword.getText().trim());
				
				jo = new JSONObject(result);
				
				JSONArray ja = jo.getJSONObject("query").getJSONArray("search");
				
				if (ja.length() > 0) {
					for (int ii = 0; ii < ja.length(); ii ++) {
						String s = ja.getJSONObject(ii).getString("title").toString();
						
						rc.add(new Object[]{s, WEIGHT_FOR_SEARCH, keyword.getLocale()});
					}
				}
			}
			
			//get translations related categories
			TranslatedKeyword tk = TranslatedKeyword.findTranslatedKeywordsByKeywordAndLocale(keyword, engLoc).getSingleResult();
			wikiLang = tk.getLocale().getShort_language();
			
			result = restTemplate.getForObject("http://" + wikiLang + ".wikipedia.org/w/api.php?action=query&prop=categories&titles={titles}&redirects=1&cllimit=20&format=json",
	                String.class, tk.getGoogleTranslation().trim());

			jo = null;
			
			try {
				jo = new JSONObject(result);
				
				j = jo.getJSONObject("query").getJSONObject("pages");
				
				if (null != j) {
					
					Iterator<String> i = (Iterator<String>)j.keys();
					
					while (i.hasNext()) {
						JSONObject oj = j.getJSONObject(i.next());
						
						try {
							System.out.println(oj.toString());
							JSONArray ja = oj.getJSONArray("categories");
							
							for (int ii = 0; ii < ja.length(); ii ++) {
								String s = ja.getJSONObject(ii).getString("title");
								String[] ss = s.split(":");
								
								rc.add(new Object[]{s, WEIGHT_FOR_CATEGORY_TRANSLATION, tk.getLocale()});
							}
						} catch (Exception ex) {
							System.out.println(ex.getMessage());
						}
						
						break;
					}
					
				} else {
					//no related categories - perform search and get titles
					
					result = restTemplate.getForObject("http://" + wikiLang + ".wikipedia.org/w/api.php?action=query&list=search&format=json&srsearch={keyword}",
			                String.class, tk.getGoogleTranslation().trim());
					
					jo = new JSONObject(result);
					
					JSONArray ja = jo.getJSONObject("query").getJSONArray("search");
					
					if (ja.length() > 0) {
						for (int ii = 0; ii < ja.length(); ii ++) {
							String s = ja.getJSONObject(ii).getString("title").toString();
							
							rc.add(new Object[]{s, WEIGHT_FOR_SEARCH_TRANSLATION, tk.getLocale()});
						}
					}
				}
			} catch (Exception ex) {
				ex.printStackTrace(System.out);
				
			}
		} catch (Exception ex) {
			ex.printStackTrace(System.out);
			
		}
		
		return rc;
	}
	
}
